package org.lg.controller;

import org.lg.entity.Transaction;
import org.lg.service.impl.TransactionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/transactions")
public class TransactionController {

    @Autowired
    private TransactionService transactionService;

    // 查询所有交易记录
    @GetMapping("/")
    public ResponseEntity<Map<String, Object>> getAllTransactions(@RequestParam String itemId) {
        List<Transaction> transactions = transactionService.getAllTransactions(itemId);
        Map<String, Object> response = new HashMap<>();
        response.put("$", transactions);  // 使用 "$" 包装交易记录
        return ResponseEntity.ok(response);
    }

    // 根据订单编号查询交易记录
    @GetMapping("/{orderNumber}")
    public ResponseEntity<Map<String, Object>> getTransactionByOrderNumber(@PathVariable String orderNumber) {
        Transaction transaction = transactionService.getTransactionByOrderNumber(orderNumber);
        Map<String, Object> response = new HashMap<>();
        response.put("transaction", transaction);  // 将交易信息包装在 "transaction" 字段中
        return ResponseEntity.ok(response);
    }

    // 添加交易记录
    @PostMapping("/")
    public ResponseEntity<Map<String, Object>> addTransaction(@RequestBody Transaction transaction) {
        String result = transactionService.addTransaction(transaction);
        Map<String, Object> response = new HashMap<>();
        response.put("message", result);  // 返回操作结果的消息
        return ResponseEntity.ok(response);
    }

        // 更新交易状态
    @PutMapping("/")
    public ResponseEntity<Map<String, Object>> updateTransactionStatus(@RequestParam String orderNumber, @RequestParam String status) {
        String result = transactionService.updateTransactionStatus(orderNumber, status);
        Map<String, Object> response = new HashMap<>();
        response.put("message", result);  // 返回操作结果的消息
        return ResponseEntity.ok(response);
    }
}
